<?php

namespace App\Listeners;

use App\Events\Event;
use App\Events\ClassInEvent;


class ClassInListener extends Listener
{

    /**
     * @param ClassInEvent $event
     */
    public function handle(ClassInEvent $event)
    {
        switch($event->action)
        {
            case ClassInEvent::WAKEUP:
                $this->wakeup($event); break;
            default: break;
        }
    }


    /**
     * 注册并添加老师到机构
     * @param TeacherEvent $event
     * @throws \Exception
     */
    protected function wakeup(ClassInEvent $event)
    {
        try{
            $this->setUri('getLoginLinked');
            $data = $this->dataMaker();
            $data["telephone"] = $event->telephone;
            $data["courseId"] = $event->courseId;
            $data["classId"] = $event->classId;
            $this->setRequest($data);
            $result = $this->callEeoApi();

            if($result['error_info']['errno'] == 1) {
                $event->setResult($result['data']);
            }else{
                throw new \Exception($result['error_info']['error'], $result['error_info']['errno']);
            }
        }catch (\Exception $e){
            throw $e;
        }

    }

}
